package com.weiwu.weiwustore.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.weiwu.weiwustore.entity.Record;
import com.weiwu.weiwustore.pojo.MaterialVo;
import com.weiwu.weiwustore.pojo.RecordVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

/**
 * 申请单
 */
@Repository
@Mapper
public interface RecordMapper extends BaseMapper<Record> {
    @Select(
            "<script>" +
                    "select mt.tname,m.mname,re.quantity,re.price from record re,material m,material_type mt" +
                    "<where>" +
                    "re.applyid = #{applicationid} and re.materialid = m.id and m.typeid = mt.id" +
                    "</where>" +
                    "</script>"
    )
    List<MaterialVo> getMaterialByapplicationid(@Param("applicationid") String applicationid);

    @Select("<script>" +
            "SELECT " +
            "    materialid,mname, type, SUM(quantity) AS total_quantity, SUM(price) AS total_price, GROUP_CONCAT(DISTINCT applyid) AS applyIds " +
            "FROM " +
            "    record r" +
            "<where>" +
            "    r.type = #{typee}" +
            "      <if test=\"storeid != null and storeid != '' \">" +
            "    and r.storeid = #{storeid} " +
            "      </if> " +
            "    <if test=\"mname != null and mname != '' \">" +
            "        and r.mname LIKE CONCAT('%', #{mname}, '%')  " +
            "    </if>" +
            "    <if test=\"beginTime != null\">" +
            "        AND r.createtime &gt;= #{beginTime} " +
            "    </if>" +
            "    <if test=\"endTime != null\">" +
            "        AND r.createtime &lt;= #{endTime} " +
            "    </if>" +
            "   GROUP BY r.materialid" +
            "      <if test=\"begin != null and size != null\">" +
            "        limit #{begin}, #{size}" +
            "      </if> " +
            "</where>" +

            "</script>")
    List<RecordVo> getmaterials(@Param("typee") String typee,
                                @Param("storeid") String storeid,
                                @Param("mname") String mname,
                                @Param("beginTime") Date beginTime,
                                @Param("endTime") Date endTime,
                                @Param("begin") Integer begin,
                                @Param("size") Integer size);
}
